#include "stdafx.h"
#include "WZLibPtr.h"

namespace WZLibPtr{
	ctor WZFile::WZFile(WZLib::WZFile* data):WZLibSmartPtr<WZLib::WZFile>(data){
	}
	WZDirectory WZFile::getDir(std::string name){
		return WZDirectory(((WZLib::WZDirectory*)this->_data->wzDir()->subDirs()[name]));
	}
	WZImage WZFile::getImg(std::string name){
		return WZImage(((WZLib::WZImage*)this->_data->wzDir()->childImages()[name]));
	}
	WZFile WZFile::Open(std::string name,bool searchSearchDirectory){
		return WZFile(WZLib::WZFile::Open(name,searchSearchDirectory));
	}
	void WZFile::free(){
		this->_data->unparse();
	}

	ctor WZDirectory::WZDirectory(WZLib::WZDirectory* data):WZLibSmartPtr<WZLib::WZDirectory>(data){
	}
	WZDirectory WZDirectory::getDir(std::string name){
		return WZDirectory(((WZLib::WZDirectory*)this->_data->subDirs()[name]));
	}
	WZImage WZDirectory::getImg(std::string name){
		return WZImage(((WZLib::WZImage*)this->_data->childImages()[name]));
	}

	ctor WZImage::WZImage(WZLib::WZImage* data):WZLibSmartPtr<WZLib::WZImage>(data){
	}
	WZImageProperty WZImage::operator [](std::string name){
		return WZImageProperty(((WZLib::WZImageProperty*)this->_data->sub()->properties()[name]));
	}
	WZImageProperty WZImage::operator [](int index){
		return WZImageProperty(((WZLib::WZImageProperty*)this->_data->sub()->properties()[index]));
	}
	int WZImage::size(){
		return this->_data->sub()->properties().size();
	}
	void WZImage::free(){
		this->_data->unparse();
	}

	ctor WZImageProperty::WZImageProperty(WZLib::WZImageProperty* data):WZLibSmartPtr<WZLib::WZImageProperty>(data){
		if(data==NULL)
			return;
		if(data->propType() == WZLib::WZImageProperty::Prop_UOL){
			this->_data=((WZLib::WZUOLProperty*)data)->resolve();
		}
		if(data->propType()/100*100 != (int)WZLib::WZImageProperty::Prop_Primitive)
			return;
		switch(data->propType()){
			case WZLib::WZImageProperty::Prop_CompressedInt:
				this->intValue=((WZLib::WZCompressedIntProperty*)data)->value;
				break;
			case WZLib::WZImageProperty::Prop_String:
				this->stringValue=((WZLib::WZStringProperty*)data)->value.c_str();
				break;
			case WZLib::WZImageProperty::Prop_Float:
				this->doubleValue=((WZLib::WZFloatProperty*)data)->value;
				break;
			case WZLib::WZImageProperty::Prop_Double:
				this->doubleValue=((WZLib::WZDoubleProperty*)data)->value;
				break;
			case WZLib::WZImageProperty::Prop_Vector:
				this->vectorValue[0]=((WZLib::WZVectorProperty*)data)->xprop->value;
				this->vectorValue[1]=((WZLib::WZVectorProperty*)data)->yprop->value;
				break;
		}
	}
	WZImageProperty WZImageProperty::operator [](std::string name){
		//first check if our property is a sub property
		if(this->_data->propType()/100*100 != (int)WZLib::WZImageProperty::Prop_Sub)
			return WZImageProperty(NULL);
		return WZImageProperty(((WZLib::WZImageProperty*)((WZLib::WZSubProperty*)this->_data)->properties()[name]));
	}
	WZImageProperty WZImageProperty::operator [](int index){
		//first check if our property is a sub property
		if(this->_data->propType()/100*100 != (int)WZLib::WZImageProperty::Prop_Sub)
			return WZImageProperty(NULL);
		return WZImageProperty(((WZLib::WZImageProperty*)((WZLib::WZSubProperty*)this->_data)->properties()[index]));
	}
	int WZImageProperty::size(){
		if(this->_data->propType()/100*100 != (int)WZLib::WZImageProperty::Prop_Sub)
			return -1;
		return ((WZLib::WZSubProperty*)this->_data)->properties().size();
	}
	WZPNGProperty WZImageProperty::png(){
#		ifdef WZLIB_HAVE_SDL
		if(this->_data->propType() != WZLib::WZImageProperty::Prop_Canvas)
			return WZPNGProperty(NULL);
		return WZPNGProperty((*(((WZLib::WZCanvasProperty*)this->_data)->png())));
#		else
		return WZPNGProperty(NULL);
#		endif
	}
};